(function() {

  var containsSize = function(str, char) {
    var pos;
    var arr = [];
    pos = str.indexOf(char);
    while (pos > -1) {
        arr.push(pos);
        pos = str.indexOf(char, pos + 1);
    }
    return arr.length;
  };

  var urlDepth = function(url) {
    url = url.replace("http://", "");
    var size = containsSize(url, "/");
    return size;
  };

  var buildBaseUrl = function(depth) {
    var url = "";
    var size = depth - 2;
    for(var i = 0; i < size; i++) {
      url += "../"
    }
    return url;
  };

  var Api = {
    post: function (path, param, _call, type) {
      $.post(PATH(path), param, function(result) {
        _call(result);
      }, type).error(function(result) {
        console.log('error', result);
        var data = result.responseJSON;
        if(result.status == 404) {
          alert("不存在该接口：" + data.path); 
          return;
        }
        _call(false);
      });
    }  
  };
  window.Api = Api;
  window.PATH = function(path) {
    // url深度
    var depth = urlDepth(location.href);
    var relativeUrl = buildBaseUrl(depth);
    // console.log('baseUrl', relativeUrl);
    return relativeUrl + path;
  };

}());